﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Collections.Generic;

namespace IZI_magic
{
    /// <summary>
    /// Main window
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<Image> images;
        private const int SMALL_CARD_WIDTH = 60;
        private const int CARD_WIDTH = 210;

        public MainWindow()
        {
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            InitializeComponent();
            images = new List<Image>();
            /*Login loginWindow = new Login();
            loginWindow.ShowDialog(); // Contrôler le retour de cette fenêtre (connexion obligatoire)
            */

            // Pour obtenir ligne/colonne d'un itemsControl (= set de cartes) :
            // Grid.GetRow(itemsControl) // GetColumn(itemsControl)
            // Récupérer l'itemsControl une fois qu'on a l'id de la ligne/colonne :
            // myGrid.Children.Cast<UIElement>().First(e => Grid.GetRow(itemsControl) == row && Grid.GetColumn(itemsControl) == column);

            BitmapImage bitmapSource = new BitmapImage();
            bitmapSource.BeginInit();
            bitmapSource.UriSource = new Uri("../../Resources/04F54DB399BEECE90CEF293A32415387.jpg", UriKind.RelativeOrAbsolute);
            bitmapSource.EndInit();

            BitmapImage bitmapSource180 = new BitmapImage();
            bitmapSource180.BeginInit();
            bitmapSource180.UriSource = new Uri("../../Resources/3F415CF855D9179147530EC3B1B1ACBF.jpg", UriKind.RelativeOrAbsolute);
            bitmapSource180.Rotation = Rotation.Rotate180;
            bitmapSource180.EndInit();

            for (int j = 0; j < 2; ++j)
            {
                BitmapImage source = (j == 0) ? bitmapSource180 : bitmapSource;

                for (int i = 1; i < 7; ++i)
                {
                    Image image = new Image();
                    image.Width = SMALL_CARD_WIDTH;
                    image.Height = source.Height * (SMALL_CARD_WIDTH / source.Width);
                    image.Source = source;
                    image.MouseEnter += new MouseEventHandler(DisplayPreview);

                    Grid.SetRow(image, j);
                    Grid.SetColumn(image, i);
                    battleField.Children.Add(image);
                }
            }

            CardList cardList = new CardList();

            Image image2 = new Image();
            image2.Width = SMALL_CARD_WIDTH;
            image2.Height = bitmapSource180.Height * (SMALL_CARD_WIDTH / bitmapSource180.Width);
            image2.Source = bitmapSource180;
            image2.MouseEnter += new MouseEventHandler(DisplayPreview);

            Image image3 = new Image();
            image3.Width = SMALL_CARD_WIDTH;
            image3.Height = bitmapSource.Height * (SMALL_CARD_WIDTH / bitmapSource.Width);
            image3.Source = bitmapSource;
            image3.MouseEnter += new MouseEventHandler(DisplayPreview);
            //image2.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            image3.Margin = new Thickness(0.0, 10.0, 0.0, 0.0);

            cardList.Add(image2);
            cardList.Add(image3); 

            ItemsControl lb = new ItemsControl();
            System.Windows.Data.Binding b1 = new System.Windows.Data.Binding();
            b1.Source = cardList;

            lb.SetBinding(ItemsControl.ItemsSourceProperty, b1);

            Grid.SetRow(lb, 4);
            Grid.SetColumn(lb, 3);
            battleField.Children.Add(lb);
        }

        private void DisplayPreview(object sender, MouseEventArgs e)
        {
            Image src = e.Source as Image;

            previewCard.Source = src.Source;
            previewCard.Height = src.Source.Height;
            previewCard.Width = CARD_WIDTH;
            previewCard.RenderTransform = src.RenderTransform; // by default (cf following code ... [rotation)
            

            /*
            // Shall we rotate the picture for the preview ?
            if (!_picture.IsOnMyBoard) // TODO : Remplacer cette condition par un test de position dans la grille. 3 premières lignes = adversaire
            {
                System.Windows.Media.ScaleTransform flipTrans = new System.Windows.Media.ScaleTransform();
                previewCard.RenderTransformOrigin = new Point(0.5, 0.5);
                flipTrans.ScaleY = -1;
                flipTrans.ScaleX = -1;
                previewCard.RenderTransform = flipTrans;
            }
            */
        }

        private void DisplayHand(object sender, RoutedEventArgs e)
        {
            HandWindow myHand = new HandWindow();
            myHand.ShowDialog();
        }
    }
}
